package org.jmc.refmanager.web.wicket.page.panel;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.jmc.refmanager.domain.dao.QueryParam;
import org.jmc.refmanager.domain.model.OrgUnit;
import org.jmc.refmanager.security.SecurityHelper;

/**
 */
public abstract class SelectParentPanel extends Panel {
	private static final long serialVersionUID = 1L;

	final QueryParam<OrgUnit> queryParams = new QueryParam<OrgUnit>();
	
	/**
	 * Methode que la page principale va implementer la persistance
	 * 
	 * @param target
	 */
	public abstract void onSave(AjaxRequestTarget target); 

	/**
	 * 
	 * @param target
	 */
	public abstract void onCancel(AjaxRequestTarget target); 

	public SelectParentPanel(String id, IModel<OrgUnit> searchModel, final IModel<OrgUnit> targetModel) {
		super(id, new CompoundPropertyModel<OrgUnit>(searchModel));
		setOutputMarkupId(true);

		final String matricule = SecurityHelper.getAuthentication().getName();

		add(new MarkupContainer("treeview") {
			private static final long serialVersionUID = 1L;
		});
		
/*		
		Form<OrgUnit> form = new Form<OrgUnit>("searchForm", searchModel);
		form.setOutputMarkupId(true);
		add(form);

		TextField<String> codePostal = new TextField<String>("codePostal");
		codePostal.setOutputMarkupId(true);
		form.add(codePostal);

		TextField<String> codeUO = new TextField<String>("codeUO");
		codeUO.setOutputMarkupId(true);
		form.add(codeUO);

		TextField<String> libCourt = new TextField<String>("libCourt");
		libCourt.setOutputMarkupId(true);
		form.add(libCourt);

		TextField<String> libLong = new TextField<String>("libLong");
		libLong.setOutputMarkupId(true);
		form.add(libLong);

		OrgUnit searchedService = (OrgUnit) getDefaultModelObject();
		queryParams.setCriteriaExample(searchedService);
		queryParams.setExcludeProperties(OrgUnitFilter.filterSearchForm());
		
		SortableServiceDataProvider dp = new SortableServiceDataProvider(queryParams, matricule);
		
		final DataView<OrgUnit> dataView = new DataView<OrgUnit>("services", dp) {
			private static final long serialVersionUID = 1L;

			@Override
		    public boolean isVisible() {
				return this.getItemCount() > 0;
			}

			@Override
			protected void populateItem(Item<OrgUnit> item) {
				final OrgUnit service = item.getModelObject();
				
				item.add(new Label("codeUO", new PropertyModel<String>(service, "codeUO")));
				
				MarkupContainer selectLink = new AjaxLink<Integer>("selectLink", new PropertyModel<Integer>(service, "id")) {
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						targetModel.getObject().setParent(service);
						// Delegation e la methode onSave()
						onSave(target);
					}
				}.add(new Label("libCourt", service.getLibCourt()));
				item.add(selectLink);
				
				item.add(new Label("libLong", service.getLibLong()));
				item.add(new Label("codePostal", service.getCodePostal()));
			}
		};

		dataView.setItemsPerPage(20);
		dataView.setOutputMarkupId(true);
		add(dataView);

		AjaxPagingNavigator navigator = new AjaxPagingNavigator("navigator", dataView) {
			private static final long serialVersionUID = 1L;

			@Override
		    public boolean isVisible() {
		        return dataView.getItemCount() > 0;
		    }
		};
		navigator.setOutputMarkupId(true);
		add(navigator);

		form.add(new AjaxButton("btnSearch", form) {
			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit(AjaxRequestTarget target, Form<?> form) {
				OrgUnit example = new OrgUnit();
				example.setCodePostal(form.get("codePostal").getDefaultModelObjectAsString());
				example.setCodeUO(form.get("codeUO").getDefaultModelObjectAsString());
				example.setLibCourt(form.get("libCourt").getDefaultModelObjectAsString());
				example.setLibLong(form.get("libLong").getDefaultModelObjectAsString());
				queryParams.setCriteriaExample(example);
				
				// Permet de rafraecher les donnees du panneau
				target.addComponent(SelectParentPanel.this);
			}
		});
		
		AjaxButton btnCancel = new AjaxButton("btnCancel", form) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				onCancel(target);
			}
		};
		// Pas de validation
		btnCancel.setDefaultFormProcessing(false);
		form.add(btnCancel);
*/
	}
	
}

